//
//  PersonneJumellageController.m
//  Cabinet
//
//  Created by Florent Montsegur on 31/12/2013.
//
//

#import "PersonneJumellageController.h"
#import "Consultation.h"
#import "Vaccination.h"

@implementation PersonneJumellageController
@synthesize Consultations;
@synthesize Vaccinations;
@synthesize mConsultationArrayController;
@synthesize mVaccinArrayController;

- (id) init
{
    if (self = [super init]){
        
        mConsultationsAjoutees = [[NSMutableArray alloc]init];
        mConsultationsSupprimees = [[NSMutableArray alloc]init];
        mVaccinationsAjoutees = [[NSMutableArray alloc]init];
        mVaccinationsSupprimees = [[NSMutableArray alloc]init];
        
        [self setConsultations:[[NSMutableArray alloc] init]];
        [self setVaccinations: [[NSMutableArray alloc] init]];
    }
    return self;
}

-(void)initArrays{
    [[self Consultations] removeAllObjects];
    [[self Vaccinations] removeAllObjects];
    
    [mConsultationsAjoutees removeAllObjects];
    [mConsultationsSupprimees removeAllObjects];
    [mVaccinationsAjoutees removeAllObjects];
    [mVaccinationsSupprimees removeAllObjects];
    
    [[self mConsultationArrayController]rearrangeObjects];
    [[self mVaccinArrayController]rearrangeObjects];
}

-(void) setPersonne:(Person*) aPersonne{
    mPersonne = aPersonne;
    
    [self initArrays];
    
    [[self Consultations] addObjectsFromArray:[[aPersonne Consultations] allObjects]];
    [[self Vaccinations] addObjectsFromArray:[[aPersonne Vaccinations] allObjects]];
    
    [[self mConsultationArrayController]rearrangeObjects];
    [[self mVaccinArrayController]rearrangeObjects];
}



-(Person*) Personne{
    return mPersonne;
}

-(void) appliquer{
    
    for(Consultation *consult in mConsultationsSupprimees)
    {
        if([[mPersonne Consultations] containsObject:consult])
            [mPersonne removeConsultationsObject:consult];
    }
    
    for(Consultation *consult in mConsultationsAjoutees)
    {
        if(![[mPersonne Consultations] containsObject:consult])
            [mPersonne addConsultationsObject:consult];
    }
    
    for(Vaccination *vac in mVaccinationsSupprimees)
    {
        if([[mPersonne Vaccinations] containsObject:vac])
            [mPersonne removeVaccinationsObject:vac];
    }
    
    for(Vaccination *vac in mVaccinationsAjoutees)
    {
        if(![[mPersonne Vaccinations] containsObject:vac])
            [mPersonne addVaccinationsObject:vac];
    }

    [self initArrays];
}


-(NSArray*) supprimerConsultations{
    
   NSArray *toReturn = [NSArray arrayWithArray:[self Consultations]];
    for(Consultation *consult in toReturn)
    {
        if(![mConsultationsSupprimees containsObject:consult])
            [mConsultationsSupprimees addObject:consult];
        
        if([mConsultationsAjoutees containsObject:consult])
            [mConsultationsAjoutees removeObject:consult];
    }
    
    [[self Consultations] removeAllObjects];
    [[self mConsultationArrayController]rearrangeObjects];
    
    return toReturn;
}

-(NSArray*) supprimerConsultationsSelectionnees{
    
    NSArray *toReturn = [NSArray arrayWithArray:[[self mConsultationArrayController] selectedObjects]];
    for(Consultation *consult in toReturn)
    {
        if(![mConsultationsSupprimees containsObject:consult])
            [mConsultationsSupprimees addObject:consult];
        
        if([mConsultationsAjoutees containsObject:consult])
            [mConsultationsAjoutees removeObject:consult];
        
        [[self Consultations] removeObject:consult];
    }
    
    [[self mConsultationArrayController]rearrangeObjects];
    
    return toReturn;
}

-(void) ajouterConsultations:(NSArray*)aConsultations{
    
    for(Consultation *consult in aConsultations)
    {
        if(![mConsultationsAjoutees containsObject:consult])
            [mConsultationsAjoutees addObject:consult];
        
        if([mConsultationsSupprimees containsObject:consult])
            [mConsultationsSupprimees removeObject:consult];
        
        if(![[self Consultations] containsObject:consult])
            [[self Consultations] addObject:consult];
    }
    
    [[self mConsultationArrayController]rearrangeObjects];
}

-(NSArray*) supprimerVaccinations{
    
    NSArray *toReturn = [NSArray arrayWithArray:[self Vaccinations]];
    for(Vaccination *vac in toReturn)
    {
        if(![mVaccinationsSupprimees containsObject:vac])
            [mVaccinationsSupprimees addObject:vac];
        
        if([mVaccinationsAjoutees containsObject:vac])
            [mVaccinationsAjoutees removeObject:vac];
    }
    
    [[self Vaccinations] removeAllObjects];
    [[self mVaccinArrayController]rearrangeObjects];
    
    return toReturn;

}

-(NSArray*) supprimerVaccinationsSelectionnees{
    
    NSArray *toReturn = [NSArray arrayWithArray:[[self mVaccinArrayController] selectedObjects]];
    for(Vaccination *vac in toReturn)
    {
        if(![mVaccinationsSupprimees containsObject:vac])
            [mVaccinationsSupprimees addObject:vac];
        
        if([mVaccinationsAjoutees containsObject:vac])
            [mVaccinationsAjoutees removeObject:vac];
        
        [[self Vaccinations] removeObject:vac];
    }
    
    [[self mVaccinArrayController]rearrangeObjects];
    
    return toReturn;
}

-(void) ajouterVaccinations:(NSArray*)aVaccinations{
    
    for(Vaccination *vac in aVaccinations)
    {
        if(![mVaccinationsAjoutees containsObject:vac])
            [mVaccinationsAjoutees addObject:vac];
        
        if([mVaccinationsSupprimees containsObject:vac])
            [mVaccinationsSupprimees removeObject:vac];
        
        if(![[self Vaccinations] containsObject:vac])
            [[self Vaccinations] addObject:vac];
    }
    
    
    [[self mVaccinArrayController]rearrangeObjects];
}

@end
